package com.fmjr.common.util;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class DateHelper
{
  public static final String BOTH = "yyyy-MM-dd HH:mm:ss";
  public static final String DATE = "yyyy-MM-dd";
  public static final String TIME = "HH:mm:ss";
  public static long MILLION_SECONDS_OF_DAY = 86400000L;

  public static long MILLION_SECONDS_OF_HOUR = 3600000L;

  public static Date parse(String str, String fmt)
  {
    Assert.notEmpty(str);
    Assert.notEmpty(fmt);
    SimpleDateFormat simDateFormat = new SimpleDateFormat(fmt);
    Date date = null;
    try {
      date = simDateFormat.parse(str);
    } catch (Exception localException) {
    }
    return date;
  }

  public static String format(Date date, String fmt)
  {
    Assert.notEmpty(date);
    Assert.notEmpty(fmt);
    SimpleDateFormat simpleDateFormat = new SimpleDateFormat(fmt);
    return simpleDateFormat.format(date);
  }

  public static Date addMonth(Date date, int months)
  {
    Calendar c = Calendar.getInstance();
    c.setTime(date);
    c.add(2, months);
    return c.getTime();
  }

  public static Date addDay(Date date, int days)
  {
	  Calendar c = Calendar.getInstance();
	  c.setTime(date);
	  c.add(5, days);
	  return c.getTime();
  }
  
  public static Date addHour(Date date,int hours)
  {
	 Calendar c = Calendar.getInstance();
	 c.setTime(date);
	 c.add(11, hours);
	 return c.getTime();
  }

  public static Date addMinutes(Date date, int minutes)
  {
	  Calendar c = Calendar.getInstance();
	  c.setTime(date);
	  c.add(12, minutes);
	  return c.getTime();
  }
  
  public static Date subMinutes(Date date, int minutes)
  {
	  Calendar c = Calendar.getInstance();
	  c.setTime(date);
	  c.add(12, -minutes);
	  return c.getTime();
  }


  public static int subDateDays(Date sd, Date ed)
  {
    Assert.notEmpty(sd);
    Assert.notEmpty(ed);
    Long eds = Long.valueOf(ed.getTime());
    Long sds = Long.valueOf(sd.getTime());
    return (int)((eds.longValue() - sds.longValue()) / MILLION_SECONDS_OF_DAY);
  }

  public static int subDateDays(String sd, String ed)
  {
    Assert.notEmpty(sd);
    Assert.notEmpty(ed);
    Long eds = Long.valueOf(parse(ed, "yyyy-MM-dd").getTime());
    Long sds = Long.valueOf(parse(sd, "yyyy-MM-dd").getTime());
    return (int)((eds.longValue() - sds.longValue()) / MILLION_SECONDS_OF_DAY);
  }
  
  public static int countDays(Date date) {
		long t = Calendar.getInstance().getTime().getTime();
		Calendar c = Calendar.getInstance();
		c.setTime(date);
		long t1 = c.getTime().getTime();
		return (int) (t / 1000 - t1 / 1000) / 3600 / 24;
	}
	public static void main(String[] args) {
		System.out.println(DateHelper.addDay(new Date(), -1));
//		System.out.println(DateHelper.format(DateHelper.addMinutes(new Date(), 3),BOTH));
		
	}
}